org.neo4j.helpers.collection
Class IteratorUtil

java.lang.Object
  extended by org.neo4j.helpers.collection.IteratorUtil

public abstract class IteratorUtil
extends Object

Contains common functionality regarding Iterators.


Constructor Summary
IteratorUtil()
           
 
Method Summary
static
<C extends Collection<T>,T>
C
addToCollection(Iterator<T> iterator, C collection)
          Adds all the items in iterator to collection.
static
<T> Iterable<T>
asIterable(Iterator<T> iterator)
          Exposes iterator as an Iterable.
static
<T> int
count(Iterator<T> iterator)
          Counts the number of items in the iterator by looping through it.
static
<T> T
singleValueOrNull(Iterator<T> iterator)
          Returns the given iterator's only value or null if there was no items in the iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IteratorUtil

public IteratorUtil()
Method Detail

singleValueOrNull

public static <T> T singleValueOrNull(Iterator<T> iterator)
Returns the given iterator's only value or null if there was no items in the iterator. If there is more than one value in the iterator an IllegalArgumentException will be thrown.

Type Parameters:
T - the type of items in iterator.
Parameters:
iterator - the Iterator to get items from.
Returns:
the only value in the iterator, or null if no value was found. Throws IllegalArgumentException if more than one value was found.

addToCollection

public static <C extends Collection<T>,T> C addToCollection(Iterator<T> iterator,
                                                            C collection)
Adds all the items in iterator to collection.

Type Parameters:
C - the type of Collection to add to items to.
T - the type of items in the collection and iterator.
Parameters:
iterator - the Iterator to grab the items from.
collection - the Collection to add the items to.
Returns:
the collection which was passed in, now filled with the items from iterator.

asIterable

public static <T> Iterable<T> asIterable(Iterator<T> iterator)
Exposes iterator as an Iterable. It breaks the contract of Iterable in that it returns the supplied iterator instance for each call to iterable() on the returned Iterable instance. This method mostly exists to make it easy to use an Iterator in a for-loop.

Type Parameters:
T - the type of items in the iterator.
Parameters:
iterator - the iterator to expose as an Iterable.
Returns:
the supplied iterator posing as an Iterable.

count

public static <T> int count(Iterator<T> iterator)
Counts the number of items in the iterator by looping through it.

Type Parameters:
T - the type of items in the iterator.
Parameters:
iterator - the Iterator to count items in.
Returns:
the number of found in iterator.


Copyright © 2010 Neo4j. All Rights Reserved.